package it.truestoryfactory.tsq.game;

import it.truestoryfactory.tsq.R;
import it.truestoryfactory.tsq.game.util.GameSession;

import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RankFragment extends ListFragment {

	private List<String> rank = new ArrayList<String>();
	private View myContentView;
	private GameSession game;
	public AlphaAnimation anima;

	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(this.getClass().getName(), "onActivity created");
		super.onActivityCreated(savedInstanceState);
		this.setListAdapter(new RankListAdapter(getActivity(), R.layout.rank_position, rank));
		((RankListAdapter) getListAdapter()).notifyDataSetChanged();
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.d(this.getClass().getName(), "onCreateView");
		myContentView = inflater.inflate(R.layout.rank_fragment, null);		
		return myContentView;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		myContentView.setEnabled(false);
		super.onViewCreated(view, savedInstanceState);
	}

	public void setRank(ArrayList<String> calculateRank, GameSession game) {
		Log.d(this.getClass().getName(), "Answers list setting");
		this.rank.clear();
		this.rank = calculateRank;
		this.game = game;
	}

	private class RankListAdapter extends ArrayAdapter<String> {

		private List<String> items;

		public RankListAdapter(Context context, int textViewResourceId, List<String> objects) {
			super(context, textViewResourceId, objects);
			Log.d(this.getClass().getName(), "Rank adapter initiated");
			items = objects;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.d(this.getClass().getName(),"Rank list adapter get view for pos " + position);
			View view = convertView;

			if (view == null) {
				LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = vi.inflate(R.layout.rank_position, parent, false);
			}

			String ip = items.get(position);

			if (ip != null) {
				TextView playerText = (TextView) view.findViewById(R.id.player);
				TextView scoreText = (TextView) view.findViewById(R.id.score);

				if (playerText != null) {
					playerText.setText((position+1) + "^ Player");
					playerText.setTextColor(game.getColors().getColorByIp(ip));
					if (ip.equals(game.myIp))
						blinkAnimation(playerText);
				}
				if (scoreText != null){
					if (ip.equals(game.myIp))
						scoreText.setText(String.valueOf(game.myScore));
					else scoreText.setText(String.valueOf(game.getScores()[game.getTable().getIndexByIp(ip)]));
					scoreText.setTextColor(game.getColors().getColorByIp(ip));
				}
			}

			return view;
		}

		private void blinkAnimation(final TextView playerText) {
			getActivity().runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					anima = new AlphaAnimation(0.0f, 1.0f);
					anima.setDuration(50);
					anima.setStartOffset(20);
					anima.setRepeatMode(Animation.REVERSE);
					anima.setRepeatCount(Animation.INFINITE);
					playerText.startAnimation(anima);					
				}
			});
		}

	}
	
	@Override
	public void onDestroy() {
		anima.cancel();
		super.onDestroy();
	}

}
